package com.minio.starter.domain;

import io.minio.messages.Item;
import io.minio.messages.Owner;

import java.time.ZonedDateTime;

/**
 * @author theOS
 * @date 2021/10/29
 */
public class MinioItem {

    private String etag;
    /**
     * 对象名称
     **/
    private String objectName;
    /**
     * 最后操作时间
     **/
    private ZonedDateTime lastModified;
    private Owner owner;
    /**
     * 对象大小
     **/
    private String size;
    private String storageClass;
    private boolean isLatest;
    private String versionId;
    /**
     * 对象类型：directory（目录）或file（文件）
     **/
    private String type;

    public MinioItem(String etag, String objectName, ZonedDateTime lastModified, Owner owner, String size, String storageClass, boolean isLatest, String versionId, String type) {
        this.etag = etag;
        this.objectName = objectName;
        this.lastModified = lastModified;
        this.owner = owner;
        this.size = size;
        this.storageClass = storageClass;
        this.isLatest = isLatest;
        this.versionId = versionId;
        this.type = type;
    }

    public MinioItem(Item item) {
        this.objectName = item.objectName();
        this.type = item.isDir() ? "directory" : "file";
        this.etag = item.etag();
        long sizeNum = item.size();
        this.size = sizeNum > 0 ? convertFileSize(sizeNum) : "0";
        this.storageClass = item.storageClass();
        this.owner = item.owner();
        this.isLatest = item.isLatest();
        this.versionId = item.versionId();
        try {
            this.lastModified = item.lastModified();
        } catch (NullPointerException e) {
        }
    }


    public String convertFileSize(long size) {
        long kb = 1024;
        long mb = kb * 1024;
        long gb = mb * 1024;
        if (size >= gb) {
            return String.format("%.1f GB", (float) size / gb);
        } else if (size >= mb) {
            float f = (float) size / mb;
            return String.format(f > 100 ? "%.0f MB" : "%.1f MB", f);
        } else if (size >= kb) {
            float f = (float) size / kb;
            return String.format(f > 100 ? "%.0f KB" : "%.1f KB", f);
        } else {
            return String.format("%d B", size);
        }
    }


    public String getEtag() {
        return etag;
    }

    public void setEtag(String etag) {
        this.etag = etag;
    }

    public String getObjectName() {
        return objectName;
    }

    public void setObjectName(String objectName) {
        this.objectName = objectName;
    }

    public ZonedDateTime getLastModified() {
        return lastModified;
    }

    public void setLastModified(ZonedDateTime lastModified) {
        this.lastModified = lastModified;
    }

    public Owner getOwner() {
        return owner;
    }

    public void setOwner(Owner owner) {
        this.owner = owner;
    }

    public String getSize() {
        return size;
    }

    public void setSize(String size) {
        this.size = size;
    }

    public String getStorageClass() {
        return storageClass;
    }

    public void setStorageClass(String storageClass) {
        this.storageClass = storageClass;
    }

    public boolean isLatest() {
        return isLatest;
    }

    public void setLatest(boolean latest) {
        isLatest = latest;
    }

    public String getVersionId() {
        return versionId;
    }

    public void setVersionId(String versionId) {
        this.versionId = versionId;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    @Override
    public String toString() {
        return "MinioItem{" +
                "etag='" + etag + '\'' +
                ", objectName='" + objectName + '\'' +
                ", lastModified=" + lastModified +
                ", owner=" + owner +
                ", size='" + size + '\'' +
                ", storageClass='" + storageClass + '\'' +
                ", isLatest=" + isLatest +
                ", versionId='" + versionId + '\'' +
                ", type='" + type + '\'' +
                '}';
    }
}
